{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Set working directory"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Changed directory to /Users/erikhemberg/Documents/MIT_PostDoc/CyberSecuritySTEALTH/Kate_Xu_2020/BRON\n",
      "Python 3.8.2\r\n"
     ]
    }
   ],
   "source": [
    "import os\n",
    "cwd = os.path.split(os.getcwd())\n",
    "if cwd[-1] == 'tutorials':\n",
    "    os.chdir('..')\n",
    "    print(f'Changed directory to {os.getcwd()}')\n",
    "!python --version"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Import modules"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "from meta_analysis.find_riskiest_software import load_graph_network, riskiest_software\n",
    "from meta_analysis.sum_unique_cves import load_graph_network, count_unique_cves\n",
    "from meta_analysis.meta_analysis_scripts.count_bron_contents import count_contents"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Extra meta-analysis\n",
    "\n",
    "This notebook contains _extra_ meta-analysis files that can be run on BRON. Make sure to first build the BRON before running meta-analyses. A tutorial on how to build BRON is available in the `tutorials` folder."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Find riskiest software\n",
    "\n",
    "This file finds the software, listed as an Affected Platform Configuration, that has the highest sum of CVSS scores in BRON. The file outputs the highest CVSS score and the software with the score. To find the riskiest software, run the following command:\n",
    "```\n",
    "python -m meta_analysis.find_riskiest_software --BRON_path BRON_PATH\n",
    "```\n",
    "`BRON_PATH` is file path of BRON."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "74.50000000000001 {'lexiglot', 'freerdp'}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(74.50000000000001, {'freerdp', 'lexiglot'})"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BRON_path = 'example_data/example_output_data/BRON.json'\n",
    "graph = load_graph_network(BRON_path)\n",
    "riskiest_software(graph)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Sum CVSS scores of unique Vulnerabilities\n",
    "\n",
    "This file sums the CVSS scores of unique Vulnerabilities that exist in BRON. To sum the CVSS scores of unique Vulnerabilities, run the following command:\n",
    "```\n",
    "python -m meta_analysis.sum_unique_cves --BRON_path BRON_PATH\n",
    "```\n",
    "`BRON_PATH` is file path of BRON."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1013.9999999999999\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "1013.9999999999999"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "BRON_path = 'example_data/example_output_data/BRON.json'\n",
    "graph = load_graph_network(BRON_path)\n",
    "count_unique_cves(graph)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Count contents of BRON\n",
    "\n",
    "This file counts the connections between data types in BRON. To count contents of BRON, run the following command:\n",
    "```\n",
    "python meta_analysis/meta_analysis_scripts/count_bron_contents.py --data_summary_folder_path DATA_SUMMARY_FOLDER_PATH --all_versions (optional) --all_years (optional)\n",
    "```\n",
    "`DATA_SUMMARY_FOLDER_PATH` is the folder path to subfolders of the data summaries for all data types. To consider all versions of Affected Platform Configurations, add the argument `--all_versions`. To consider all years of Vulnerability data, add the argument `--all_years`.\n",
    "\n",
    "It is important for the data summary folder containing subfolders to have the following subfolder names:\n",
    "```\n",
    "* all_cves_all_versions\n",
    "* recent_cves_all_versions\n",
    "* all_cves_latest_version\n",
    "* recent_cves_latest_version\n",
    "```\n",
    "\n",
    "Each of the subfolders should contain data summaries for all threat data types:\n",
    "```\n",
    "* tactic_summary.csv\n",
    "* technique_summary.csv\n",
    "* capec_summary.csv\n",
    "* cwe_summary.csv\n",
    "* cve_summary.csv\n",
    "* cpe_summary.csv\n",
    "```\n",
    "Refer to `meta_analysis/make_data_summary.py` to create data summaries for all threat data types."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tactic dict  {'floating': 0, 'above_only': 12, 'below_only': 0, 'both': 0}\n",
      "technique dict  {'floating': 1, 'above_only': 195, 'below_only': 0, 'both': 70}\n",
      "capec dict  {'floating': 128, 'above_only': 14, 'below_only': 325, 'both': 52}\n",
      "cwe dict  {'floating': 0, 'above_only': 212, 'below_only': 9, 'both': 30}\n",
      "cve dict  {'floating': 73, 'above_only': 0, 'below_only': 4, 'both': 140}\n",
      "cpe dict  {'floating': 0, 'above_only': 277, 'below_only': 0, 'both': 0}\n"
     ]
    }
   ],
   "source": [
    "data_summary_folder_path = 'example_data/example_output_data'\n",
    "all_versions = True\n",
    "all_years = True\n",
    "count_contents(data_summary_folder_path, all_versions, all_years)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "@webio": {
   "lastCommId": null,
   "lastKernelId": null
  },
  "kernelspec": {
   "display_name": "venv_BRON",
   "language": "python",
   "name": "venv_bron"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
